//
//  BVUser.m
//  AFNetworking
//
//  Created by 崔晋 on 2019/8/20.
//

#import "BVUser.h"
#import "BVUserDefault.h"
#import "BVService.h"

@implementation BVBaiduFace



@end

@implementation BVFace



@end

static BVUser *USER = nil;

@implementation BVUser

+ (JSONKeyMapper *)keyMapper {
    return [[JSONKeyMapper alloc] initWithModelToJSONDictionary:@{@"token": @"token", @"baiduFace": @"baiduFace", @"face": @"face"}];
}


+ (instancetype)sharedInstanceWith:(NSDictionary *)dic {
    NSError *error;
    [[BVUserDefault standardUserDefaults] setValue:dic[@"data"][@"token"] forKey:BV_LOGIN_USER_TOKEK_DEFAULT_KEY];
    USER = [[BVUser alloc] initWithDictionary:dic[@"data"] error:&error];
    [[BVService shareInstance] get:@"profile" param:@{} complete:^(BOOL success, id  _Nullable response) {
        if (success) {
            BVUserInfoModel *userModel = [[BVUserInfoModel alloc] initWithDictionary:response[@"data"] error:nil];
            USER.model = userModel;
            NSDictionary *dic11 = [USER toDictionary];
            [dic11 writeToFile:UserInfoFilePath atomically:YES];
        }
    }];
    return USER;
}

+ (BOOL)propertyIsIgnored:(NSString *)propertyName {
    if ([propertyName isEqualToString:@"isLogin"]) {
        return YES;
    }
    return NO;
}
+ (instancetype)shareInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!USER) {
            if ([[NSFileManager defaultManager] fileExistsAtPath:UserInfoFilePath]) {
                NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:UserInfoFilePath];
                USER = [[BVUser alloc] initWithDictionary:dic error:nil];
            }else {
                USER = [[BVUser alloc] init];
            }
        }
    });
    return USER;
}

- (BOOL)isLogin {
    USER.token = [[BVUserDefault standardUserDefaults] valueForKey:BV_LOGIN_USER_TOKEK_DEFAULT_KEY];
    return [[BVUserDefault standardUserDefaults] boolForKey:BV_LOGIN_USER_DEFAULT_KEY];
}

@end
